package cn.itcast.wushangxiantu.Fragment;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.Fragment;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.core.content.ContextCompat;

import com.google.gson.Gson;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.IOException;

import cn.itcast.wushangxiantu.UserData.UserBag;
import cn.itcast.wushangxiantu.Activity.Login;
import cn.itcast.wushangxiantu.MessageEvent;
import cn.itcast.wushangxiantu.OkhttpUtil;
import cn.itcast.wushangxiantu.R;
import cn.itcast.wushangxiantu.ToastUtil;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

@SuppressLint("ValidFragment")
public class LingShiZhuangbei extends Fragment implements View.OnClickListener, View.OnLongClickListener {
    private View view;
    private TextView tvBiaoTi;
    private TextView tvLs1;
    private TextView tvLs2;
    private TextView tvLsDj;
    private TextView tvLs3;
    private TextView tvLs4;
    private Context context;
    static int num = 0;
    private LinearLayout ll1;
    private LinearLayout ll2;
    private LinearLayout ll3;
    private LinearLayout ll4;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_lingshizhuangbei, null);
        EventBus.getDefault().register(this);
        initView(view);
        return view;
    }

    @SuppressLint("ValidFragment")
    public LingShiZhuangbei(Context context) {
        this.context = context;
    }

    public static int getNum() {
        return num;
    }

    public static void setNum(int num) {
        LingShiZhuangbei.num = num;
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    private void initView(View view) {
        tvBiaoTi = (TextView) view.findViewById(R.id.tvBiaoTi);
        tvLs1 = (TextView) view.findViewById(R.id.tvLs1);
        tvLs2 = (TextView) view.findViewById(R.id.tvLs2);
        tvLsDj = (TextView) view.findViewById(R.id.tvLsDj);
        tvLs3 = (TextView) view.findViewById(R.id.tvLs3);
        tvLs4 = (TextView) view.findViewById(R.id.tvLs4);

        tvLs1.setOnClickListener(this);
        tvLs2.setOnClickListener(this);
        tvLs3.setOnClickListener(this);
        tvLs4.setOnClickListener(this);

        tvLs1.setOnLongClickListener(this);
        tvLs2.setOnLongClickListener(this);
        tvLs3.setOnLongClickListener(this);
        tvLs4.setOnLongClickListener(this);
        ll1 = (LinearLayout) view.findViewById(R.id.ll1);
        ll1.setOnClickListener(this);
        ll2 = (LinearLayout) view.findViewById(R.id.ll2);
        ll2.setOnClickListener(this);
        ll3 = (LinearLayout) view.findViewById(R.id.ll3);
        ll3.setOnClickListener(this);
        ll4 = (LinearLayout) view.findViewById(R.id.ll4);
        ll4.setOnClickListener(this);
        setLingshi();
    }

    public String name(int num) {
        if (num == 0) {
            return "空";
        }
        String name[] = {"一","二","三","四","五","六","七","八","九","十",
                "十一","十二","十三","十四","十五","十六","十七","十八","十九","二十","二十一","二十二","二十三","二十四","二十五"};
        return name[num-1] + "级灵石";
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public void setLingshi() {
        tvLs1.setText(name(Login.getUser().getLs1()));
        tvLs2.setText(name(Login.getUser().getLs2()));
        tvLs3.setText(name(Login.getUser().getLs3()));
        tvLs4.setText(name(Login.getUser().getLs4()));
        setColor(tvLs1,Login.getUser().getLs1());
        setColor(tvLs2,Login.getUser().getLs2());
        setColor(tvLs3,Login.getUser().getLs3());
        setColor(tvLs4,Login.getUser().getLs4());
        tvLsDj.setText("灵石等级：" + (Login.getUser().getLs1() + Login.getUser().getLs2() + Login.getUser().getLs3() + Login.getUser().getLs4()));
    }

    @Override
    public void onClick(View view) {
        tvLs1.setBackgroundResource(R.drawable.border4);
        tvLs2.setBackgroundResource(R.drawable.border4);
        tvLs3.setBackgroundResource(R.drawable.border4);
        tvLs4.setBackgroundResource(R.drawable.border4);
        switch (view.getId()) {
            case R.id.tvLs1:
                num = 0;
                tvLs1.setBackgroundResource(R.drawable.draw_5);
                break;
            case R.id.tvLs2:
                num = 1;
                tvLs2.setBackgroundResource(R.drawable.draw_5);
                break;
            case R.id.tvLs3:
                num = 2;
                tvLs3.setBackgroundResource(R.drawable.draw_5);
                break;
            case R.id.tvLs4:
                tvLs4.setBackgroundResource(R.drawable.draw_5);
                num = 3;
                break;
        }
    }

    @Override
    public boolean onLongClick(View view) {
        tvLs1.setBackgroundResource(R.drawable.border4);
        tvLs2.setBackgroundResource(R.drawable.border4);
        tvLs3.setBackgroundResource(R.drawable.border4);
        tvLs4.setBackgroundResource(R.drawable.border4);
        switch (view.getId()) {
            case R.id.tvLs1:
                num = 0;
                if (!tvLs1.getText().toString().equals("空")) {
                    GeneralDialog("卸下灵石", "确定要卸下灵石吗？\n这样会损失很多属性");
                }
                tvLs1.setBackgroundResource(R.drawable.draw_5);
                break;
            case R.id.tvLs2:
                num = 1;
                if (!tvLs2.getText().toString().equals("空")) {
                    GeneralDialog("卸下灵石", "确定要卸下灵石吗？\n这样会损失很多属性");
                }
                tvLs2.setBackgroundResource(R.drawable.draw_5);
                break;
            case R.id.tvLs3:
                num = 2;
                if (!tvLs3.getText().toString().equals("空")) {
                    GeneralDialog("卸下灵石", "确定要卸下灵石吗？\n这样会损失很多属性");
                }
                tvLs3.setBackgroundResource(R.drawable.draw_5);
                break;
            case R.id.tvLs4:
                tvLs4.setBackgroundResource(R.drawable.draw_5);
                num = 3;
                if (!tvLs4.getText().toString().equals("空")) {
                    GeneralDialog("卸下灵石", "确定要卸下灵石吗？\n这样会损失很多属性");
                }
                break;
        }
        return false;
    }

    public void GeneralDialog(String head, String content) {
        /**
         * 普通对话框
         * 1标题
         * 2内容
         */
        AlertDialog dialog;
        AlertDialog.Builder builder = new AlertDialog.Builder(context)
                .setTitle(head)

                .setMessage(content)
                .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        getDate(Login.getUser().getUsernumber(), num + 1);
                    }
                });
        dialog = builder.create();
        dialog.show();
    }

    private void getDate(final String userName, final int weizhi) {
        OkhttpUtil.getRequest(OkhttpUtil.URL_LOG + "userName=" + userName + "&weizhi=" + weizhi + "&need=xiexialingshi", new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.v("xiexia1", e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String src = response.body().string();
                Log.v("xiexia2", src);
                UserBag.BeibaoBean bean = null;
                try {
                    bean = new Gson().fromJson(src, UserBag.BeibaoBean.class);
                }catch (Exception e){

                }
                Message message = new Message();
                message.what = 1;
                message.obj = bean;
                handler.sendMessage(message);
            }
        });

    }

    Handler handler = new Handler(new Handler.Callback() {
        @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
        @Override
        public boolean handleMessage(@NonNull Message message) {
            UserBag.BeibaoBean bean = (UserBag.BeibaoBean) message.obj;
            if (message.what == 1) {
               if (bean!=null){
                   if (bean.getStw() == null) {
                       switch (num) {
                           case 0:
                               tvLs1.setText("空");
                               setColor(tvLs1,0);
                               Login.getUser().setLs1(0);
                               break;
                           case 1:
                               tvLs2.setText("空");
                               setColor(tvLs2,0);
                               Login.getUser().setLs2(0);
                               break;
                           case 2:
                               tvLs3.setText("空");
                               setColor(tvLs3,0);
                               Login.getUser().setLs3(0);
                               break;
                           case 3:
                               tvLs4.setText("空");
                               setColor(tvLs4,0);
                               Login.getUser().setLs4(0);
                               break;
                       }
                       tvLsDj.setText("灵石等级：" + (Login.getUser().getLs1() + Login.getUser().getLs2() + Login.getUser().getLs3() + Login.getUser().getLs4()));
                       EventBus.getDefault().post(new MessageEvent("Lingshi"));
                   } else {
                       ToastUtil.toast(context, bean.getStw());
                   }
               }else {
                   ToastUtil.toast(context,"服务器挂了");
               }
            }
            return false;
        }
    });

    /**
     * 接收后处理
     */
    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void shuaxing(MessageEvent messageEvent) {
        if (messageEvent.getMessage().equals("LingshiZhuangbei")) {
            switch (num) {
                case 0:
                    tvLs1.setText(name(messageEvent.getNum()));
                    Login.getUser().setLs1(messageEvent.getNum());
                    setColor(tvLs1,Login.getUser().getLs1());
                    break;
                case 1:
                    tvLs2.setText(name(messageEvent.getNum()));
                    Login.getUser().setLs2(messageEvent.getNum());
                    setColor(tvLs2,Login.getUser().getLs2());
                    break;
                case 2:
                    tvLs3.setText(name(messageEvent.getNum()));
                    Login.getUser().setLs3(messageEvent.getNum());
                    setColor(tvLs3,Login.getUser().getLs3());
                    break;
                case 3:
                    tvLs4.setText(name(messageEvent.getNum()));
                    Login.getUser().setLs4(messageEvent.getNum());
                    setColor(tvLs4,Login.getUser().getLs4());
                    break;
            }
            tvLsDj.setText("灵石等级：" + (Login.getUser().getLs1() + Login.getUser().getLs2() + Login.getUser().getLs3() + Login.getUser().getLs4()));

        }
    }
    @SuppressLint("ResourceAsColor")
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public void setColor(TextView textView, int num){
        switch (num){
            case 1:
            case 2:
            case 3:
                textView.setTextColor(ContextCompat.getColor(context,R.color.lingshi1));
                break;
            case 4:
            case 5:
            case 6:
                textView.setTextColor(ContextCompat.getColor(context,R.color.lingshi2));
                break;
            case 7:
            case 8:
            case 9:
                textView.setTextColor(ContextCompat.getColor(context,R.color.lingshi3));
                break;
            case 10:
            case 11:
            case 12:
                textView.setTextColor(ContextCompat.getColor(context,R.color.lingshi4));
                break;
            case 13:
            case 14:
            case 15:
                textView.setTextColor(ContextCompat.getColor(context,R.color.lingshi5));
                break;
            case 16:
            case 17:
            case 18:
                textView.setTextColor(ContextCompat.getColor(context,R.color.lingshi6));
                break;
            case 19:
            case 20:
            case 21:
                textView.setTextColor(ContextCompat.getColor(context,R.color.lingshi7));
                break;
            case 22:
            case 23:
            case 24:
                textView.setTextColor(ContextCompat.getColor(context,R.color.lingshi8));
                break;
            case 25:
                textView.setTextColor(ContextCompat.getColor(context,R.color.lingshi9));
                break;
            case 0:
                textView.setTextColor(ContextCompat.getColor(context,R.color.baise));
        }
    }
}
